//
//  QLVideoPlayer.m
//  QLCustomCamera_Example
//
//  Created by 李强 on 2020/9/1.
//  Copyright © 2020 李强LiQ. All rights reserved.
//

#import "QLVideoPlayer.h"
#import "sys/utsname.h"
#import "QLConfigure.h"

@interface QLVideoPlayer ()<ZFPlayerDelegate>

@property (nonatomic,strong)UIView *fatherView;
@property (nonatomic,strong)ZFPlayerView *player;

@end

@implementation QLVideoPlayer

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
#pragma clang diagnostic pop

}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
#pragma clang diagnostic pop

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    self.fatherView = [UIView new];
    self.fatherView.frame = CGRectMake(0, [self tz_isIPhoneX] ? 24 : 0, QL_SCREEN_WIDTH, [self tz_isIPhoneX] ? QL_SCREEN_HEIGHT - 24 - 34 : QL_SCREEN_HEIGHT);
    [self.view addSubview:self.fatherView];
    
    self.model.fatherView = self.fatherView;
    self.model.placeholderImage = [self getImageWithColor:[UIColor blackColor] andHeight:0];
    
    self.player = [[ZFPlayerView alloc]init];
    self.player.delegate = self;
//    self.player.forcePortrait = YES;
//    self.player.fullScreenPlay = YES;
    [self.player playerModel:self.model];
    [self.player autoPlayTheVideo];
}

//获取视频
-(UIImage *)getImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r = CGRectMake(0.0f, 0.0f, QL_SCREEN_WIDTH, QL_SCREEN_HEIGHT);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

-(void)zf_playerBackAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(CGFloat)tz_statusBarHeight
{
    return [self tz_isIPhoneX] ? 44 : 20;
}


//MARK:---------lazy------------
-(ZFPlayerModel *)model
{
    if (!_model) {
        _model = [ZFPlayerModel new];
    }
    return _model;
}


//是否支持自动转屏
-(BOOL)shouldAutorotate
{
    return NO;
}

//支持的屏幕方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

//默认的屏幕方向
//（当前ViewController必须是通过模态出来的UIViewController（模态带导航的无效）方式展现出来的，才会调用这个方法）
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}


- (BOOL)tz_isIPhoneX {
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
    if ([platform isEqualToString:@"i386"] || [platform isEqualToString:@"x86_64"]) {
        // 模拟器下采用屏幕的高度来判断
        return (CGSizeEqualToSize([UIScreen mainScreen].bounds.size, CGSizeMake(375, 812)) ||
                CGSizeEqualToSize([UIScreen mainScreen].bounds.size, CGSizeMake(812, 375)));
    }
    // iPhone10,6是美版iPhoneX 感谢hegelsu指出：https://github.com/banchichen/TZImagePickerController/issues/635
    BOOL IPhoneX = [platform isEqualToString:@"iPhone10,3"] || [platform isEqualToString:@"iPhone10,6"];
    return IPhoneX;
}











@end
